package com.agriculture.data_demo.repository;

import com.agriculture.data_demo.entity.sys.BroilerEnvEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author morningbear
 * @date 20.10.7
 */
@Repository
public interface BroilerEnvRepository extends JpaRepository<BroilerEnvEntity, Integer> {

    @Query(value = "select * from broiler_env", nativeQuery = true)
    Page<BroilerEnvEntity> findAll(PageRequest of);

    /**
     *
     * @param fence 鸡舍
     * @return 根据鸡舍查询
     */
    @Query(value = "select * from broiler_env where fence=?1", nativeQuery = true)
    Page<BroilerEnvEntity> findByFence(String fence, Pageable pageable);

    /**
     *
     * @return 温度大于平均值的栏位
     */
    @Query(value = "select * from broiler_env where temp > (select avg(temp) from broiler_env)", nativeQuery = true)
    Page<BroilerEnvEntity> findBroilerEnvEntitiesByTemperature(Pageable pageable);

    /**
     *
     * @param startDate 查询起始日期
     * @param endDate 查询结束日期
     * @return 查询时间段中的肉鸡环境数据
     */
    @Query(value = "select * from broiler_env where broiler_env.create_at between ?1 and ?2", nativeQuery = true)
    Page<BroilerEnvEntity> findBroilerEnvEntitiesByDate(Date startDate, Date endDate, Pageable pageable);
}
